package cn.wangliang181230.json.parser;

import cn.wangliang181230.json.parser.fastjson.FastJSONParser;
import cn.wangliang181230.json.parser.fastjson2.FastJSON2Parser;
import cn.wangliang181230.json.parser.jackson.JacksonParser;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;

public class JsonAutoConfiguration {

	public JsonAutoConfiguration() {
		System.out.println("JsonAutoConfiguration: Constructor");
	}


	@Bean
	public static JsonBeanPostProcessor jsonBeanPostProcessor() {
		System.out.println("JsonAutoConfiguration: jsonBeanPostProcessor");
		return new JsonBeanPostProcessor();
	}


	@Bean
	@ConditionalOnClass(name = "com.alibaba.fastjson2.JSON")
	@ConditionalOnProperty(prefix = "my.json", value = "type", havingValue = "fastjson2", matchIfMissing = true)
	@ConditionalOnMissingBean
	//@ConditionalOnMissingBean(IJSONParser.class) // 效果同上一行
	public IJSONParser fastJSON2Parser() {
		System.out.println("JsonAutoConfiguration: fastJSON2Parser");
		return new FastJSON2Parser();
	}

	@Bean
	@ConditionalOnClass(name = "com.alibaba.fastjson.JSON")
	@ConditionalOnProperty(prefix = "my.json", value = "type", havingValue = "fastjson", matchIfMissing = true)
	@ConditionalOnMissingBean
	//@ConditionalOnMissingBean(IJSONParser.class) // 效果同上一行
	public IJSONParser fastJSONParser() {
		System.out.println("JsonAutoConfiguration: fastJSONParser");
		return new FastJSONParser();
	}

	@Bean
	@ConditionalOnClass(name = "com.fasterxml.jackson.databind.ObjectMapper")
	@ConditionalOnProperty(prefix = "my.json", value = "type", havingValue = "jackson", matchIfMissing = true)
	@ConditionalOnMissingBean
	//@ConditionalOnMissingBean(IJSONParser.class) // 效果同上一行
	public IJSONParser jacksonParser() {
		System.out.println("JsonAutoConfiguration: jacksonParser");
		return new JacksonParser();
	}

}
